package cn.shyouchi.wuguanjia.android.ui.patrol.begin;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import cn.shyouchi.wuguanjia.android.Global;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.impl.ModuleItemClickListener;

import static cn.shyouchi.wuguanjia.android.R.id.tv_check_patrol_detail;

/**
 * Created by Eenie on 2016/12/28 at 10:27
 * Email: 472279981@qq.com
 * Des:
 */

public class PatrolRingDetailAdapter extends BaseQuickAdapter<PatrolRingDesEntity.ResultMessageBean.InfoBean, BaseViewHolder> {


    ModuleItemClickListener mItemClickListener;

    public PatrolRingDetailAdapter(List<PatrolRingDesEntity.ResultMessageBean.InfoBean> data, ModuleItemClickListener listener) {
        super(R.layout.item_patrol_detail_layout, data);
        this.mItemClickListener = listener;
    }

    @Override
    protected void convert(BaseViewHolder holder, final PatrolRingDesEntity.ResultMessageBean.InfoBean entity) {


        if (entity.getInspection() != null && entity.getInspection().getId() != 0) {
            holder.setBackgroundRes(R.id.tv_position, entity.getInspection().getSituation() == 0 ? R.drawable.shape_circle_red : R.drawable.shape_circle_blue);
            holder.getView(R.id.img_take_photo).setVisibility(View.INVISIBLE);
            holder.getView(tv_check_patrol_detail).setVisibility(View.VISIBLE);
            holder.setText(R.id.tv_report_time2, String.format("%1s", entity.getInspection().getTime()));

            holder.setText(R.id.tv_report_post2, String.format("%1s",  entity.getInspection().getSituation() == 0 ? "异常" : "正常"));
            holder.setTextColor(R.id.tv_report_post2, entity.getInspection().getSituation() == 0 ? Color.RED : Color.parseColor("#666666"));

            List<String> imgs = entity.getInspection().getImage();

            if (!imgs.isEmpty()) {
                try {
                    JSONObject img = new JSONObject(imgs.get(0));


                    loadHead(Global.BASE_URL + img.getString("image"), (ImageView) holder.getView(R.id.img_report));


                } catch (JSONException e) {
                    e.printStackTrace();

                }
            }







        } else {
            holder.getView(R.id.img_take_photo).setVisibility(View.VISIBLE);
            holder.getView(tv_check_patrol_detail).setVisibility(View.INVISIBLE);
            holder.setBackgroundRes(R.id.tv_position, R.drawable.shape_circle_gray);
            holder.setTextColor(R.id.tv_report_post2, Color.parseColor("#333333"));




        }


        holder.setText(R.id.tv_position, String.valueOf(holder.getAdapterPosition() + 1));
        holder.setText(R.id.tv_report_post, entity.getInspectiontime());
        holder.setText(R.id.tv_report_time, entity.getInspectionname());

        ImageView imageView = holder.getView(R.id.img_patrol);

        if (entity.getInspectionmethod() == 2) {
            if (!entity.getImage().isEmpty()) {
                try {
                    String p = new JSONObject(entity.getImage().get(0)).getString("image");
                    loadHead(Global.BASE_URL + p, imageView);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        } else {
            imageView.setImageResource(R.mipmap.ic_patrol_item_qrcode);
        }


        holder.getView(R.id.img_take_photo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemClickListener.moduleItemClick(entity);
            }
        });
        holder.getView(tv_check_patrol_detail).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, PreviewPatrolDetailActivity.class);
                intent.putExtra("info", entity);
                mContext.startActivity(intent);

            }
        });

        holder.getView(R.id.tv_check_detail).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, PreviewPatrolRingDetailActivity.class);
                intent.putExtra("info", entity);

                mContext.startActivity(intent);




            }
        });
    }


    private void loadHead(String path, ImageView imageView) {
        Glide.with(mContext)
                .load(path)
                .into(imageView);
    }
}
